<?php
namespace Cyfy;

/**
 * Logging.
 *
 * Writing logs and error messages into \Cyfy\Cyfy ::  $logDir directory.
 * Logs are saved only if \Cyfy\Cyfy :: $debug is true.
 * Errors and warnings are saved always.
 *
 * @category cyfy
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Log
{	
    /**
     * Filename of log file without extension.
     * @var string
     */
    static public $filename = "log";
    
    /**
     * Display notices
     * @var bool
     */
    static public $notices = false;
    
    /**
     * Internal buffer
     * @var string
     */
    static private $buffer = "";
    
    /**
     * True, if HTML log is enabled.
     * @var bool
     */
    static private $html = false;
    
    /**
     * True, if logs are saved in the file
     * @var bool
     */
    static private $append = false;
    
    /**
     * Microtime
     * @var int
     */
    static public $timeStart = 0; 
     
    /**
     * Method that starts logging.
     *
     * Method that starts logging. Inserts a header into the file.
     *
     * @param bool HTML log enabled
     * @param bool Append text to one file
     */
    static public function start($html = false, $append = false)
    {
        self :: $html = $html;
        self :: $append = $append;
        self :: $buffer = "<h1>" . date("Y-m-d H:i:s") . ": STARTING TO GENERATE CYFY" . "</h1>";
        self :: $timeStart = microtime(true);
    }
    
    /**
     * Writes text into buffer.
     *
     * Writes text into log buffer. HTML tag can specify the content when log is in HTML mode.
     * When debug mode is not on, nothing will be written.
     *
     * @param mixed Content, if string, then serialized via print_r.
     * @param string HTML tag without &gt; &lt;, eg. 'strong' for &gt;strong&lt;
     */
    static public function write($text, $tag = "p")
    {
        if(is_array($text)){
            $text = print_r($text, 1);
            $tag = "pre";
        }
        
        $time = (microtime(true) - self :: $timeStart);
        $text = $time . " | " . $text;
        
        if(self :: $html)
        {
            $text = "<{$tag}>{$text}</{$tag}>";
        }
        self :: $buffer .= $text . "\n";
        
    }
    
    /**
     * Writes error text into buffer.
     *
     * Writes text into log buffer. HTML tag can specify the content when log is in HTML mode.
     * This message is written, even when debug mode is off.
     * The message and HTML tag are in DIV tag, with class "error".
     *
     * @param string Content
     * @param string HTML tag without &gt; &lt;, eg. 'strong' for &gt;strong&lt;
     */
    static public function writeError($text, $tag = "p")
    {
        $time = (microtime(true) - self :: $timeStart);
        
        $text = $time . " | " . $text;
        
        if(self :: $html)
        {
            $text = "<div class=\"error\"><{$tag}>{$text}</{$tag}></div>";
        }
        self :: $buffer .= $text . "\n";
    }
    
    /**
     * Saves the log into the file.
     *
     * @return TRUE, when save is successful.
     */
    static public function save()
    {
        // get microtime
        self :: write("___ cyfy generated in " . (microtime(true) - self :: $timeStart) . " seconds.");
        
        // some settings
        $ext = (self :: $html) ? ".html" : ".txt";
        $flags = (self :: $append) ? FILE_APPEND : 0;
        return file_put_contents(\Cyfy\Cyfy ::  $logDir . self :: $filename . $ext . "", self :: $buffer . "\n", $flags);
    }
    
    /**
     * Error handling function.
     *
     * Error handling function. Used for PHP calls.
     *
     * @param string Error number
     * @param string Error message
     * @param string File, which caused the error
     * @param string Line, on the error is
     * @param string Variables used in the file
     * @return TRUE
     */
    static public function errorHandler($errno, $errstr, $errfile, $errline, $errcontent)
    {
        switch ($errno)
        {
            case E_ERROR:
            case E_USER_ERROR:
                self :: writeError("<b>ERROR</b> [$errno] $errstr", "h2");
                self :: writeError("File $errfile, line $errline.");
                self :: writeError(print_r($errcontent, 1), "pre");
                self :: writeError("");
                include("_end.php");
                break;

            case E_WARNING:
            case E_USER_WARNING:
                self :: writeError("<b>WARNING</b> [$errno] $errstr", "h2");
                self :: writeError("File $errfile, line $errline.");
                self :: writeError(print_r($errcontent, 1), "pre");
                self :: writeError("");
                break;

            case E_NOTICE:
            case E_USER_NOTICE:
                if(!(self :: $notices)) break;
                self :: write("<b>NOTICE</b> [$errno] $errstr", "h3");
                self :: write("File $errfile, line $errline.");
                break;

            default:
                self :: writeError("$errstr", "h2");
                self :: writeError("File $errfile, line $errline.");
                self :: writeError("",true);
                break;
        }

        /* Don't execute PHP internal error handler */
        return true;
    }
}
